package com.rndemo.db;

import android.database.Cursor;

import com.rndemo.annotations.FieldTag;
import com.rndemo.annotations.FieldType;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tzj on 2018/5/21.
 * TODO 这里用到的 反射可以，用map 保存优化
 */
public class BaseReflex {
    @FieldTag(type = FieldType.ignore)
    protected Map<String, String> objFiles = new HashMap<>();

    /**
     * 创建数据库语句
     */
    protected Map<String, String> toDb(Class<?> c) {
        if (c == BaseReflex.class) {
            return objFiles;
        }
        Field[] fields = c.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                int modifiers = field.getModifiers();
                if ((modifiers & Modifier.STATIC) > 0) {
                    continue;
                }
                FieldTag annotation = field.getAnnotation(FieldTag.class);
                String value = field.getType().getSimpleName();
                if (annotation != null) {
                    FieldType type = annotation.type();
                    value = type.name();
                    if (type == FieldType.ignore) {
                        continue;
                    } else if (type == FieldType.VARCHAR) {
                        value = "varchar(" + annotation.length() + ")";
                    }
                    if (annotation.isKey()) {
                        value += " primary key";
                    }
                } else {
                    if (field.getType() == String.class) {
                        value = "text";
                    }else if(field.getType() == Date.class){
                        value = "long";
                    }
                }
                objFiles.put(field.getName(), value.toLowerCase());
            }
        }
        c = c.getSuperclass();
        return toDb(c);
    }

    /**
     * 将类转成map
     */
    protected Map<String, Object> toValue(Class<?> c, Map<String, Object> map) {
        if (c == BaseReflex.class) {
            return map;
        }
        Field[] fields = c.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                int modifiers = field.getModifiers();
                if ((modifiers & Modifier.STATIC) > 0) {
                    continue;
                }
                FieldTag annotation = field.getAnnotation(FieldTag.class);
                if (annotation != null && annotation.type() == FieldType.ignore) {
                    continue;
                }
                field.setAccessible(true);
                try {
                    Object value = null;
                    if (field.getType() == Date.class) {//这里date 类型转换成long 型
                        value = ((Date) field.get(this)).getTime();
                    } else {
                        value = field.get(this);
                    }
                    map.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        c = c.getSuperclass();
        return toValue(c, map);
    }

    /**
     * 类填充value
     */
    protected void filling(Object obj, Class c, Cursor cursor) {
        if (c == BaseReflex.class) {
            return;
        }
        Field[] fields = c.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                int modifiers = field.getModifiers();
                if ((modifiers & Modifier.STATIC) > 0) {
                    continue;
                }
                FieldTag annotation = field.getAnnotation(FieldTag.class);
                if (annotation != null && annotation.type() == FieldType.ignore) {
                    continue;
                }
                field.setAccessible(true);
                Class<?> type = field.getType();
                int columnIndex = cursor.getColumnIndex(field.getName());
                Object value = null;
                if (type == String.class) {
                    value = cursor.getString(columnIndex);
                } else if (type == Integer.class || type == Integer.TYPE) {
                    value = cursor.getInt(columnIndex);
                } else if (type == Boolean.class || type == Boolean.TYPE) {//这里强转
                    String i = cursor.getString(columnIndex);
                    value = Boolean.valueOf(i);
                } else if (type == Byte.class || type == Byte.TYPE) {
                    value = (byte)cursor.getInt(columnIndex);
                } else if (type == Float.class || type == Float.TYPE) {
                    value = cursor.getFloat(columnIndex);
                } else if (type == Long.class || type == Long.TYPE) {
                    value = cursor.getLong(columnIndex);
                } else if (type == Double.class || type == Double.TYPE) {
                    value = cursor.getDouble(columnIndex);
                } else if (type == Date.class) {//这里要转换
                    long time = cursor.getLong(columnIndex);
                    value = new Date(time);
                }
                try {
                    field.set(obj, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        c = c.getSuperclass();
        filling(obj, c, cursor);
    }
}
